<?php
/**
 * @version 1.2
 * @author Misha Klomp
 * @editor Sander van Belleghem
 * 
 */

class DbControle {
    
    public function checkString($string, $max, $min) {
        
        //controleer of het een string is
        if (!is_string($string)) {
            throw new Exception(DBCONTROLE_ERROR_GEENSTRING);
        }

        // check max en min
        if (strlen($string) > $max) {
            throw new Exception(DBCONTROLE_ERROR_STRINGTELANG);
        }

        if (strlen($string) < $min) {
            throw new Exception(DBCONTROLE_ERROR_STRINGTEKORT);
            
        } else {
            return true;
        }
    }
    
    public function checkInt($int, $max, $min) {
        
        //controleer of het een int is
        if (!is_int($int)) {
            throw new Exception(DBCONTROLE_ERROR_GEENINT);
        }

        // check max en min
        if ($int > $max) {
            throw new Exception(DBCONTROLE_ERROR_INTTEHOOG);
        }

        if ($int < $min) {
            throw new Exception(DBCONTROLE_ERROR_INTTELAAG);
            
        } else {
            return true;
        }
    }
    
    public function checkId($id){
        
        return $this->checkInt($id, PHP_INT_MAX, 1);
    }

    public function __destruct(){
        //$this->disconnect();
    }
    
    // Invoer is altijd nl datum
    public function CheckDate($new){
        if(strpos($new, '/')){
            $new = str_replace('/', '-', $new);
        }
        
        list($day, $month, $year) = explode("-", $new);

        if (checkdate($month, $day, $year))
        {
            return true;
        }
        
        throw new Exception(DBCONTROLE_ERROR_GEENDATUM);
    }
    
    public function mysql_datetime($datetime){
        
        list($day, $month, $year) = explode("-", $datetime);
        
        list($year, $time) = explode(" ", $year);
        
        list($uur, $minuut, $seconde) = explode(":", $time);
        
        return $year . "-" . $month . "-" . $day . " " . $time;
    }
    
    public function dutch_datetime($datetime){
        
        list($year, $month, $day) = explode("-", $datetime);
        
        list($day, $time) = explode(" ", $day);
        
        list($uur, $minuut, $seconde) = explode(":", $time);
        
        return $day . "-" . $month . "-" . $year . " " . $time;
    }
    
    // Opgeven van nederlandse datum volgens format 00-00-0000
    // Uitvoer MySql datum
    public function mysql_date($date){

        list($day, $month, $year) = explode("/", $date);
        
        return $year . "-" . $month . "-" . $day;
    }
    
    // Opgeven MySql datum volgens format 0000-00-00
    // Uitvoer Nederlandse datum
    public function dutch_date($date){
        
       list($year, $month, $day) = explode("-", $date);
       
       return $day . "-" . $month . "-" . $year;
    }
}
?>